"""
from sympy import symbols, sqrt, diff, acos
from sympy.simplify import ratsimp
from sympy.simplify.cse_main import cse

x1, y1, z1 = symbols('x1 y1 z1', real=True)
x2, y2, z2 = symbols('x2 y2 z2', real=True)
xa, ya, za = symbols('xa ya za', real=True)

nx1 = y1 * za - z1 * ya
ny1 = z1 * xa - x1 * za
nz1 = x1 * ya - y1 * xa

nx2 = y2 * za - z2 * ya
ny2 = z2 * xa - x2 * za
nz2 = x2 * ya - y2 * xa

vn1xvaX = ny1*za - nz1*ya
vn1xvaY = nz1*xa - nx1*za
vn1xvaZ = nx1*ya - ny1*xa

vn1xv1X = ny1*z1 - nz1*y1
vn1xv1Y = nz1*x1 - nx1*z1
vn1xv1Z = nx1*y1 - ny1*x1

vn1xv2X = ny1*z2 - nz1*y2
vn1xv2Y = nz1*x2 - nx1*z2
vn1xv2Z = nx1*y2 - ny1*x2

vn2xvaX = ny2*za - nz2*ya
vn2xvaY = nz2*xa - nx2*za
vn2xvaZ = nx2*ya - ny2*xa

vn2xv2X = ny2*z2 - nz2*y2
vn2xv2Y = nz2*x2 - nx2*z2
vn2xv2Z = nx2*y2 - ny2*x2

vn2xv1X = ny2*z1 - nz2*y1
vn2xv1Y = nz2*x1 - nx2*z1
vn2xv1Z = nx2*y1 - ny2*x1


r1 = sqrt(nx1*nx1+ny1*ny1+nz1*nz1)
r2 = sqrt(nx2*nx2+ny2*ny2+nz2*nz2)
r12 = sqrt((nx1*nx1 + ny1*ny1 + nz1*nz1)*(nx2*nx2 + ny2*ny2 + nz2*nz2))

dotN12 = (nx1*nx2+ny1*ny2+nz1*nz2)
cost = dotN12/sqrt((nx1*nx1+ny1*ny1+nz1*nz1)*(nx2*nx2+ny2*ny2+nz2*nz2))

symbols = {r12:'r12', dotN12:'dotN12', r1:'r1', r2:'r2', 
    vn1xvaX:'vn1xvaX', vn1xvaY:'vn1xvaY', vn1xvaZ:'vn1xvaZ',
    vn1xv1X:'vn1xv1X', vn1xv1Y:'vn1xv1Y', vn1xv1Z:'vn1xv1Z',
    vn2xvaX:'vn2xvaX', vn2xvaY:'vn2xvaY', vn2xvaZ:'vn2xvaZ',
    vn2xv2X:'vn2xv2X', vn2xv2Y:'vn2xv2Y', vn2xv2Z:'vn2xv2Z',
    (nz2*ya - ny2*za):'-vn2xvaX', (nx2*za - nz2*xa):'-vn2xvaY', (ny2*xa - nx2*ya):'-vn2xvaZ',
    (nz1*ya - ny1*za):'-vn1xvaX', (nx1*za - nz1*xa):'-vn1xvaY', (ny1*xa - nx1*ya):'-vn1xvaZ',
    (nz1*y1 - ny1*z1):'-vn1xv1X', (nx1*z1 - nz1*x1):'-vn1xv1Y', (ny1*x1 - nx1*y1):'-vn1xv1Z',
    (nz2*y2 - ny2*z2):'-vn2xv2X', (nx2*z2 - nz2*x2):'-vn2xv2Y', (ny2*x2 - nx2*y2):'-vn2xv2Z',
    (ny1*z2 + ny2*z1 - nz1*y2 - nz2*y1):'(vn1xv2X + vn2xv1X)',
    (nz1*x2 + nz2*x1 - nx1*z2 - nx2*z1):'(vn1xv2Y + vn2xv1Y)',
    (nx1*y2 + nx2*y1 - ny1*x2 - ny2*x1):'(vn1xv2Z + vn2xv1Z)',
    nx1:'nx1', ny1:'ny1', nz1:'nz1', 
    nx2:'nx2', nz2:'nz2', nz2:'nz2',
    xa*z2 - x2*za:'ny2',
    (-3*nz1*ya + 3*ny1*za):'3*vn1xvaX',  (-3*nx1*za + 3*nz1*xa):'3*vn1xvaY',  (-3*ny1*xa + 3*nx1*ya):'3*vn1xvaZ',
    (-3*ny1*z1 + 3*nz1*y1):'-3*vn1xv1X', (-3*nz1*x1 + 3*nx1*z1):'-3*vn1xv1Y', (-3*nx1*y1 + 3*ny1*x1):'-3*vn1xv1Z', 
    (-3*ny2*z2 + 3*nz2*y2):'-3*vn2xv2X', (-3*nz2*x2 + 3*nx2*z2):'-3*vn2xv2Y', (-3*nx2*y2 + 3*ny2*x2):'-3*vn2xv2Z', 
}


print "dx[0].x =", diff(cost, x1).subs(symbols)
print "dx[0].y =", diff(cost, y1).subs(symbols)
print "dx[0].z =", diff(cost, z1).subs(symbols)

print "dx[1].x =", diff(cost, x2).subs(symbols)
print "dx[1].y =", diff(cost, y2).subs(symbols)
print "dx[1].z =", diff(cost, z2).subs(symbols)

print "dx[2].x =", diff(cost, xa).subs(symbols)
print "dx[2].y =", diff(cost, ya).subs(symbols)
print "dx[2].z =", diff(cost, za).subs(symbols)


print "dx2[0][0][0] =", cost.diff(x1, x1).subs(symbols)
print "dx2[0][0][1] =", cost.diff(x1, y1).subs(symbols)
print "dx2[0][0][2] =", cost.diff(x1, z1).subs(symbols)
print "dx2[0][1][0] =", cost.diff(y1, x1).subs(symbols)
print "dx2[0][1][1] =", cost.diff(y1, y1).subs(symbols)
print "dx2[0][1][2] =", cost.diff(y1, z1).subs(symbols)
print "dx2[0][2][0] =", cost.diff(z1, x1).subs(symbols)
print "dx2[0][2][1] =", cost.diff(z1, y1).subs(symbols)
print "dx2[0][2][2] =", cost.diff(z1, z1).subs(symbols)

print "dx2[1][0][0] = dx2[3][0][0] = ", cost.diff(x1, x2).subs(symbols)
print "dx2[1][0][1] = dx2[3][1][0] = ", cost.diff(x1, y2).subs(symbols)
print "dx2[1][0][2] = dx2[3][2][0] = ", cost.diff(x1, z2).subs(symbols)
print "dx2[1][1][0] = dx2[3][0][1] = ", cost.diff(y1, x2).subs(symbols)
print "dx2[1][1][1] = dx2[3][1][1] = ", cost.diff(y1, y2).subs(symbols)
print "dx2[1][1][2] = dx2[3][2][1] = ", cost.diff(y1, z2).subs(symbols)
print "dx2[1][2][0] = dx2[3][0][2] = ", cost.diff(z1, x2).subs(symbols)
print "dx2[1][2][1] = dx2[3][1][2] = ", cost.diff(z1, y2).subs(symbols)
print "dx2[1][2][2] = dx2[3][2][2] = ", cost.diff(z1, z2).subs(symbols)

print "dx2[2][0][0] = dx2[6][0][0] = ", cost.diff(x1, xa).subs(symbols)
print "dx2[2][0][1] = dx2[6][1][0] = ", cost.diff(x1, ya).subs(symbols)
print "dx2[2][0][2] = dx2[6][2][0] = ", cost.diff(x1, za).subs(symbols)
print "dx2[2][1][0] = dx2[6][0][1] = ", cost.diff(y1, xa).subs(symbols)
print "dx2[2][1][1] = dx2[6][1][1] = ", cost.diff(y1, ya).subs(symbols)
print "dx2[2][1][2] = dx2[6][2][1] = ", cost.diff(y1, za).subs(symbols)
print "dx2[2][2][0] = dx2[6][0][2] = ", cost.diff(z1, xa).subs(symbols)
print "dx2[2][2][1] = dx2[6][1][2] = ", cost.diff(z1, ya).subs(symbols)
print "dx2[2][2][2] = dx2[6][2][2] = ", cost.diff(z1, za).subs(symbols)

print "dx2[4][0][0] =", cost.diff(x2, x2).subs(symbols)
print "dx2[4][0][1] =", cost.diff(x2, y2).subs(symbols)
print "dx2[4][0][2] =", cost.diff(x2, z2).subs(symbols)
print "dx2[4][1][0] =", cost.diff(y2, x2).subs(symbols)
print "dx2[4][1][1] =", cost.diff(y2, y2).subs(symbols)
print "dx2[4][1][2] =", cost.diff(y2, z2).subs(symbols)
print "dx2[4][2][0] =", cost.diff(z2, x2).subs(symbols)
print "dx2[4][2][1] =", cost.diff(z2, y2).subs(symbols)
print "dx2[4][2][2] =", cost.diff(z2, z2).subs(symbols)


print "dx2[5][0][0] = dx2[7][0][0] = ", cost.diff(x2, xa).subs(symbols)
print "dx2[5][0][1] = dx2[7][1][0] = ", cost.diff(x2, ya).subs(symbols)
print "dx2[5][0][2] = dx2[7][2][0] = ", cost.diff(x2, za).subs(symbols)
print "dx2[5][1][0] = dx2[7][0][1] = ", cost.diff(y2, xa).subs(symbols)
print "dx2[5][1][1] = dx2[7][1][1] = ", cost.diff(y2, ya).subs(symbols)
print "dx2[5][1][2] = dx2[7][2][1] = ", cost.diff(y2, za).subs(symbols)
print "dx2[5][2][0] = dx2[7][0][2] = ", cost.diff(z2, xa).subs(symbols)
print "dx2[5][2][1] = dx2[7][1][2] = ", cost.diff(z2, ya).subs(symbols)
print "dx2[5][2][2] = dx2[7][2][2] = ", cost.diff(z2, za).subs(symbols)

print "dx2[8][0][0] =", cost.diff(xa, xa).subs(symbols)
print "dx2[8][0][1] =", cost.diff(xa, ya).subs(symbols)
print "dx2[8][0][2] =", cost.diff(xa, za).subs(symbols)
print "dx2[8][1][0] =", cost.diff(ya, xa).subs(symbols)
print "dx2[8][1][1] =", cost.diff(ya, ya).subs(symbols)
print "dx2[8][1][2] =", cost.diff(ya, za).subs(symbols)
print "dx2[8][2][0] =", cost.diff(za, xa).subs(symbols)
print "dx2[8][2][1] =", cost.diff(za, ya).subs(symbols)
print "dx2[8][2][2] =", cost.diff(za, za).subs(symbols)

"""


data = {
    '(-3*nz1*ya + 3*ny1*za)':'3*vn1xvaX', '(-3*nx1*za + 3*nz1*xa)':'3*vn1xvaY', '(-3*ny1*xa + 3*nx1*ya)':'3*vn1xvaZ',
    '(-3*ny1*z1 + 3*nz1*y1)':'-3*vn1xv1X', '(-3*nz1*x1 + 3*nx1*z1)':'-3*vn1xv1Y','(-3*nx1*y1 + 3*ny1*x1)':'-3*vn1xv1Z', 
    '(-3*ny2*z2 + 3*nz2*y2)':'-3*vn2xv2X', '(-3*nz2*x2 + 3*nx2*z2)':'-3*vn2xv2Y','(-3*nx2*y2 + 3*ny2*x2)':'-3*vn2xv2Z', 
    '(-3*nz2*ya + 3*ny2*za)':'3*vn2xvaX', '(-3*nx2*za + 3*nz2*xa)':'3*vn2xvaY', '(-3*ny2*xa + 3*nx2*ya)':'3*vn2xvaZ',
}

with open('cost2.txt') as file:
    s = file.read()

for k, v in data.iteritems():
    s = s.replace(k, v)
print s

